package com.firexxx.detect;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.firexxx.detect.monitor.JavaHeadParser;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Threads;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

public class Helloworld {

    public static AtomicInteger atomicInteger = new AtomicInteger(1);
    public static ReentrantLock lock = new ReentrantLock();
    public static Object syncObj = new Object();

    public void test() throws JsonProcessingException {
        int a = 1;
        a++;
    }

    public synchronized void testSync() throws JsonProcessingException {
        int a = 1;
        a++;
    }

    public void testSync1() throws JsonProcessingException {
//        System.out.println(Thread.currentThread().getName() + JavaHeadParser.parse(syncObj).toJson());
        int a = 1;
        synchronized (syncObj) {
//            System.out.println(Thread.currentThread().getName() + JavaHeadParser.parse(syncObj).toJson());
            a++;
        }
//        System.out.println(Thread.currentThread().getName() + JavaHeadParser.parse(syncObj).toJson());
    }

    public void testAtomic() {
        atomicInteger.addAndGet(1);
    }

    public void testLock() {
        int a = 1;
        lock.lock();
        a++;
        lock.unlock();
    }

    @Benchmark
    @Threads(10)
//    @BenchmarkMode(Mode.AverageTime)
    public void normal() throws JsonProcessingException {
//        System.out.println(JavaHeadParser.parse(syncObj).toJson());
//        testSync();
//        testSync1();
//        System.out.println(JavaHeadParser.parse(syncObj).toJson());
//        testSync1();
//        System.out.println(JavaHeadParser.parse(syncObj).toJson());
//        testAtomic();
        test();
    }

    @Benchmark
    @Threads(10)
//    @BenchmarkMode(Mode.AverageTime)
    public void sync() throws JsonProcessingException {
//        System.out.println(JavaHeadParser.parse(syncObj).toJson());
//        testSync();
        testSync1();
//        System.out.println(JavaHeadParser.parse(syncObj).toJson());
//        testSync1();
//        System.out.println(JavaHeadParser.parse(syncObj).toJson());
//        testAtomic();
//        testLock();
    }

    @Benchmark
    @Threads(10)
//    @BenchmarkMode(Mode.AverageTime)
    public void lock() throws JsonProcessingException {
//        System.out.println(JavaHeadParser.parse(syncObj).toJson());
//        testSync();
//        testSync1();
//        System.out.println(JavaHeadParser.parse(syncObj).toJson());
//        testSync1();
//        System.out.println(JavaHeadParser.parse(syncObj).toJson());
//        testAtomic();
        testLock();
    }

    @Benchmark
    @Threads(10)
//    @BenchmarkMode(Mode.AverageTime)
    public void atomic() throws JsonProcessingException {
//        System.out.println(JavaHeadParser.parse(syncObj).toJson());
//        testSync();
//        testSync1();
//        System.out.println(JavaHeadParser.parse(syncObj).toJson());
//        testSync1();
//        System.out.println(JavaHeadParser.parse(syncObj).toJson());
        testAtomic();
//        testLock();
    }

}
